#pragma once
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<typeinfo>
#include<exception>
#include<stdexcept>

#include<arpa/inet.h>
#include<netinet/ip.h>
#include<signal.h>
#include<sys/select.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>

using namespace std;
#define BackLog 3

class TcpServer
{
    public:
      TcpServer() = delete;
      TcpServer(const TcpServer& t) = delete;
      TcpServer& operator=(const TcpServer& t) = delete;
      static int Socket()
      {
          int lsock = socket(AF_INET,SOCK_STREAM,0);
          if( lsock < 0 )
          {
             throw runtime_error(string("socket error"));
          }
          cout<<"Socket:lsock:"<<lsock<<endl;
          return lsock;
      }
      static void Bind(int lsock,int port)
      {
          sockaddr_in local;
          local.sin_family = AF_INET;
          local.sin_port = htons(port);//注意网络字节序
          local.sin_addr.s_addr = INADDR_ANY;

          if( bind(lsock,(struct sockaddr*)&local,sizeof(local)) < 0 )
          {
             throw runtime_error(string("bind error"));
          }
          cout<<"Bind success..."<<endl;

      }
      static void Listen(int lsock)
      {
          if( listen(lsock,BackLog) < 0 )
          {
              throw runtime_error(string("listen error")); 
          }
          cout<<"Listen success..."<<endl;
      }
      static int Accept(int lsock)
      {
          sockaddr_in end_point;
          socklen_t len = sizeof(end_point);
          int sock = accept(lsock,(struct sockaddr*)&end_point,&len);
          if( sock < 0 )
          {
              cout<<"this time accept fail ..."<<endl;
              return -1;
          }
          else{
              return sock;
          }
      }
      static void SetSockAddr(int lsock)
      {
          int opt =1 ; 
          setsockopt( lsock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt) );
      }
      ~TcpServer()
      {}
    private:

};

